package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.job.api.model.Payload;
import org.jeecg.modules.job.ums.entity.UmsChatHistory;
import org.jeecg.modules.job.ums.mapper.UmsChatHistoryMapper;
import org.jeecg.modules.job.ums.service.IUmsChatHistoryService;
import org.jeecg.modules.job.utils.JsonUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 聊天记录
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class UmsChatHistoryServiceImpl extends ServiceImpl<UmsChatHistoryMapper, UmsChatHistory> implements IUmsChatHistoryService {

    @Override
    public void addBatch(List<UmsChatHistory> param) {
        for (UmsChatHistory item:param) {
            item.setSendTime(new Date(item.getTimestamp()));
            //进行转换
            if (StringUtils.isNotEmpty(item.getPayload())){
                Payload payload= JsonUtils.jsonToPojo(item.getPayload(), Payload.class);
                if ("text".equals(item.getType())){
                    item.setTexts(payload.getText());
                }else if ("image".equals(item.getType())){
                    item.setName(payload.getName());
                    item.setUrl(payload.getUrl());
                    item.setWidth(payload.getWidth());
                    item.setHeight(payload.getHeight());
                    item.setSize(payload.getSize());
                }else if ("audio".equals(item.getType())){
                    item.setName(payload.getName());
                    item.setUrl(payload.getUrl());
                    item.setDuration(payload.getDuration());
                    item.setSize(payload.getSize());
                }else if ("video".equals(item.getType())){
                    item.setName(payload.getVideo().getName());
                    item.setUrl(payload.getVideo().getUrl());
                    item.setDuration(payload.getVideo().getDuration());
                    item.setSize(payload.getVideo().getSize());
                    item.setCoverUrl(payload.getThumbnail().getUrl());
                }
            }
            this.add(item);
        }
    }

    @Override
    public void add(UmsChatHistory param) {
        //去重添加
        UmsChatHistory result=this.getOne(new QueryWrapper<>(new UmsChatHistory().setMessageId(param.getMessageId())));
        if (result!=null){
            System.err.println(result.getMessageId()+"已添加，请勿重复。");
        }else{
            this.save(param);
        }
    }

    @Override
    public IPage<Map<String, Object>> getChatList(Page<UmsChatHistory> page, UmsChatHistory params) {
        return baseMapper.getChatList(page,params);
    }
}
